package com.hwpt.hwaccount.feature.security;

import android.content.Context;

import com.hwpt.hwaccount.core.data.local.entity.UserEntity;
import com.hwpt.hwaccount.core.data.repository.UserInfoRepository;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SecurityModel implements SecurityContract.Model {
    private final UserInfoRepository repository;
    private final CompositeDisposable disposables;

    public SecurityModel(Context context) {
        this.repository = new UserInfoRepository(context);
        this.disposables = new CompositeDisposable();
    }

    @Override
    public void getUserInfo(SecurityContract.Model.DataCallback<UserEntity> callback) {
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        callback.onSuccess(users.get(0));
                    } else {
                        callback.onError("用户信息不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void updateUserInfo(UserEntity user, SecurityContract.Model.DataCallback<Boolean> callback) {
        disposables.add(repository.insertUser(user)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(() -> {
                    callback.onSuccess(true);
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void changePassword(String currentPassword, String newPassword, SecurityContract.Model.DataCallback<Boolean> callback) {
        // 模拟密码修改逻辑
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        // 这里应该调用API修改密码
                        callback.onSuccess(true);
                    } else {
                        callback.onError("用户不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void setupEmergencyContact(String name, String phone, String relationship, SecurityContract.Model.DataCallback<Boolean> callback) {
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        UserEntity user = users.get(0);
                        user.setContactName(name);
                        user.setContactMobile(phone);
                        user.setRelationship(relationship);

                        repository.insertUser(user)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(() -> {
                                    callback.onSuccess(true);
                                }, throwable -> {
                                    callback.onError(throwable.getMessage());
                                });
                    } else {
                        callback.onError("用户不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void sendPhoneVerifyCode(String phone, SecurityContract.Model.DataCallback<Boolean> callback) {
        // 模拟发送验证码
        disposables.add(repository.setLoggedIn(false) // 只是借用这个方法模拟网络请求
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(() -> {
                    callback.onSuccess(true);
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void bindPhone(String phone, String code, SecurityContract.Model.DataCallback<Boolean> callback) {
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        UserEntity user = users.get(0);
                        user.setPhone(phone);

                        repository.insertUser(user)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(() -> {
                                    callback.onSuccess(true);
                                }, throwable -> {
                                    callback.onError(throwable.getMessage());
                                });
                    } else {
                        callback.onError("用户不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void unbindPhone(SecurityContract.Model.DataCallback<Boolean> callback) {
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        UserEntity user = users.get(0);
                        user.setPhone(null);

                        repository.insertUser(user)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(() -> {
                                    callback.onSuccess(true);
                                }, throwable -> {
                                    callback.onError(throwable.getMessage());
                                });
                    } else {
                        callback.onError("用户不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void sendEmailVerifyCode(String email, SecurityContract.Model.DataCallback<Boolean> callback) {
        // 模拟发送邮箱验证码
        disposables.add(repository.setLoggedIn(false)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(() -> {
                    callback.onSuccess(true);
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    @Override
    public void bindEmail(String email, String code, SecurityContract.Model.DataCallback<Boolean> callback) {
        disposables.add(repository.getAllUsers()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(users -> {
                    if (users != null && !users.isEmpty()) {
                        UserEntity user = users.get(0);
                        user.setEmail(email);

                        repository.insertUser(user)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(() -> {
                                    callback.onSuccess(true);
                                }, throwable -> {
                                    callback.onError(throwable.getMessage());
                                });
                    } else {
                        callback.onError("用户不存在");
                    }
                }, throwable -> {
                    callback.onError(throwable.getMessage());
                }));
    }

    public void dispose() {
        disposables.clear();
    }
}